package com.shanghaionstar.navi.search.result;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.amap.api.services.help.Tip;
import com.onstar.cn.framework.mvp.base.core.AppScheduler;
import com.onstar.cn.framework.mvp.base.core.BaseFragment;
import com.onstar.cn.framework.utils.RxBus;
import com.onstar.cn.map.util.ToastUtil;
import com.shanghaionstar.R;
import com.shanghaionstar.nav.service.AbstractMapActivity;
import com.shanghaionstar.navi.listener.OnItemClickListener;
import com.shanghaionstar.navi.search.result.mvpview.SearchResultView;
import com.shanghaionstar.navi.search.result.presenter.SearchResultPresenter;
import com.shanghaionstar.utils.MapHandler;

import java.util.ArrayList;

import rx.functions.Action1;

/**
 * Created by zhaoyang on 2017/7/21.
 */

public class NavSearchResultFragment extends BaseFragment implements SearchResultView {

    private RecyclerView mResultRv;
    private TextView mNoResultTipTv;

    private AbstractMapActivity mMapActivity;
    private MapHandler mMapHandler;
    private FragmentManager mFragManager;

    private NavSearchResultAdapter mResultAdapter;
    private SearchResultPresenter mResultPresenter;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_nav_search_result;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initPresenter();
        registerResultData();
    }

    private void initPresenter() {
        mResultPresenter = new SearchResultPresenter();
        mResultPresenter.attach(getActivity(), this);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        mNoResultTipTv = (TextView) getContentView().findViewById(R.id.no_result_tip_tv);

        initResultRv();
    }

    // after onActivityCreated
    private void initResultRv() {
        mResultAdapter = new NavSearchResultAdapter(getActivity().getApplicationContext());
        mResultRv = (RecyclerView) getContentView().findViewById(R.id.search_result_rv);
        mResultRv.setLayoutManager(new LinearLayoutManager(getActivity().getApplicationContext()));
        mResultRv.setAdapter(mResultAdapter);

        mResultAdapter.setOnItemClickListener(new OnItemClickListener<Tip>() {
            @Override
            public void onItemClick(Tip item) {
                ToastUtil.showShort(getActivity(), item.getDistrict());
                mMapActivity.hideSoftInput();
            }
        });
    }

    private void registerResultData() {
        RxBus.getDefault()
                .toObserverable(888, Bundle.class)
                .subscribeOn(new AppScheduler().backgroundThread())
                .observeOn(new AppScheduler().mainThread())
                .subscribe(new Action1<Bundle>() {
                    @Override
                    public void call(Bundle bundle) {
                        if (bundle == null) return;

                        mNoResultTipTv.setVisibility(View.GONE);
                        mResultRv.setVisibility(View.VISIBLE);
                        ArrayList<Tip> resultList = bundle.getParcelableArrayList("tips_list");
                        mResultAdapter.setData(resultList);
                    }
                });

        RxBus.getDefault().toObserverable(999, String.class)
                .subscribeOn(new AppScheduler().backgroundThread())
                .observeOn(new AppScheduler().mainThread())
                .subscribe(new Action1<String>() {
                    @Override
                    public void call(String errorTip) {
                        mNoResultTipTv.setText(errorTip);
                        mResultRv.setVisibility(View.GONE);
                        mNoResultTipTv.setVisibility(View.VISIBLE);
                    }
                });
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mMapActivity = (AbstractMapActivity) activity;
        mFragManager = mMapActivity.getSupportFragmentManager();
        mMapHandler = mMapActivity.getMapHandler();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        if (hidden && mResultAdapter != null)
            mResultAdapter.clearData();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        if (mResultPresenter != null)
            mResultPresenter.detach();
    }
}
